Let's take a closer
look at CNN's now. CNN's consists of
multiple layers, each performing a
specific task and extracting features
or predicting labels. One of the principal
layer types is a convolutional layer that extracts important
features in images. A convolutional layer works by applying a filter to images. The filter is defined
by a kernel that consists of a matrix
of weight values. For example, a three-by-three
filter might be defined with value
choices of minus 1, 0, 1. An image is also just a
matrix of pixel values. To apply the filter, you overlay it on an image and calculate a weighted sum of the corresponding
image pixel values under the filter kernel. The result is then assigned
to the center cell of the equivalent
three-by-three patch in a new matrix of values that is the same size as the image. For example, suppose
a six-by-six image has pixel values of either 100 or 255 for each grid cell, applying the filter
to the top-left, three-by-three patch of
the image would yield a result of 155. The result is assigned to the corresponding pixel
value in the new matrix. Now the filter is moved
along or convolved, typically using a
step size of one, so moving along one
pixel to the right, then the value for the
next pixel is calculated. Now we can fill in the next
value of the new matrix. The process repeats until we've applied the filter across all of the three-by-three
patches of the image to produce a new
matrix of values. Because of the size
of the filter kernel, we can't calculate values
for the pixels at the edge, so we typically just apply a padding value
which is often zero. The output of the convolution is typically passed to an
activation function, which is often a rectified
linear unit function that ensures negative
values are set to zero. The resulting matrix
is a feature map of feature values that can be used to train a machine
learning model. It's important to note that the values in the
feature map can be greater than the maximum value
for a pixel which is 255. If you wanted to visualize
the feature map as an image, you would need to normalize the feature values
between 0 and 255. Let's take a look at the
convolution process now. First, an image is passed
to the convolution layer. In this case, the image is
a simple geometric shape. Next, the image is
composed of an array of pixels with values
between 0 and 255. For color images,
this is usually a three-dimensional array
with values for red, green, and blue channels. Then a filter kernel is generally initialized
with random weights. In this example, we've
chosen values to highlight the effect that a filter
might have on pixel values. But in a real CNN, the initial weights
would typically be generated from a random
Gaussian distribution. This filter will be used to extract a feature map
from the image data. The filter is convolved
across the image, calculating feature values by applying a sum of the weights multiplied by their
corresponding pixel values in each position. A rectified linear unit
activation function is applied to ensure negative
values are set to zero. After convolution, the
feature map contains the extracted feature
values which often emphasize key visual
attributes of the image. In this case, the
feature map highlights the edges and corners of
the triangle in the image. Typically, a convolution layer applies multiple filter kernels. Each filter produces a
different feature map, and all of the feature maps are passed on to the next
layer of the network. After extracting feature
values from images, pooling or downsampling layers are used to reduce the number of feature values while
retaining the key differentiating features
that have been extracted. One of the most common
pooling is max pooling, in which a filter is
applied to the image and only the maximum pixel value within the filter
area is retained. For example, applying a
two-by-two pooling kernel to the following patch
of an image would produce the result 155. Note that the effect of the
two-by-two pooling filter is to reduce the number of
values from four to one. As with convolutional layers, pooling layers work by applying the filter across the
whole feature map, the feature map
extracted by a filter in a convolutional layer contains an array of feature values. A pooling kernel is used to reduce the number
of feature values. In this case, the kernel
size is two-by-two so we'll produce an array with quarter the number
of feature values. The pooling kernel is convolved
across the feature map, retaining only the highest
pixel value in each position. The one of the most
difficult challenges in a CNN is the avoidance of overfitting where
the resulting model performs well with
the training data, but doesn't generalize well to new data on which
it wasn't trained. One technique you
can use to mitigate overfitting is to
include layers in which the training
process randomly eliminates or drops
feature maps. This may seem counter-intuitive, but it's an effective way
to ensure that the model doesn't learn to
be over-dependent on the training images. Other techniques you
can use to mitigate overfitting include
randomly flipping, mirroring or skewing
the training images to generate data that varies
between training epochs. After using convolutional
and pooling layers to extract the salient
features in the images, the resulting feature maps are a multi-dimensional
arrays of pixel values. A flattening layer is used to flatten the feature maps into a vector of values
that can be used as input to a fully
connected layer. Usually a CNN ends with a fully connected network in which feature values
are passed into an input layer through one or more hidden
layers and generate predicted values in
an output layer. Let's explore basic
CNN architecture. First, images are fed into
a convolutional layer. In this case, there
are two filters so each image produces
two feature maps. The feature maps are passed
to a pooling layer where a two-by-two pooling kernel reduces the size of
the feature maps. Then a dropping
layer randomly drops some of the feature maps to
help prevent overfitting. A flattening layer takes the remaining feature map arrays and flattens them into a vector. Finally, the vector
elements are fed into a fully connected network which generates the predictions. In this case, the network is
a classification model that predicts probabilities for
three possible image classes; triangles, square, and circle. As with any deep neural network, a CNN is trained by
passing batches of training data through it
over multiple epochs. Adjusting the weights
and bias values based on the loss
calculated for each epoch. In the case of a CNN, backpropagation of
adjusted weights includes filter
kernel weights used in convolutional
layers as well as the weights used in
fully connected layers.